package com.blb.day20231103hellostarter.service;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Hello服务的自动配置类
 */
//EnableConfigurationProperties启动后，可以通过构造方法注入对象
@EnableConfigurationProperties(HelloProperties.class)
@Configuration
public class HelloServiceAutoConfiguration {

    private HelloProperties helloProperties;

    public HelloServiceAutoConfiguration(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }

    /**
     * 完成HelloService服务的自动配置
     * @Bean 将返回对象交给IOC容器
     * @return
     */
    @Bean
    //如果容器中没有helloService对象，就执行配置方法，保证对象的单例
    @ConditionalOnMissingBean
    public HelloService helloService(){
        HelloService helloService = new HelloService(helloProperties.getName(),
                helloProperties.getAge(),
                helloProperties.getGender());
        return helloService;
    }
}
